<template>
  <entity-view
    :operator-type="operatorType"
    :model="beanInfo"
    @submit="submitSec"
    :rules="rules"
    ref="form"
  >
    <div class="sub-content-flex">
      <form-item
        label="安全计划类型"
        v-model="beanInfo.keyType"
        prop="keyType"
        disabled
        :inputType="select"
        :options="keyTypeList"
        @change="(value) => handleChange(value, 'keyType')"
      ></form-item>
      <form-item
        label="安全计划操作方式"
        v-model="beanInfo.keyOprType"
        prop="keyOprType"
        :inputType="select"
        :options="keyOprTypeList"
        @change="(value) => handleChange(value, 'keyOprType')"
      ></form-item>
      <form-item
        label="安全计划用途"
        v-model="beanInfo.keyUsage"
        prop="keyUsage"
        :inputType="select"
        :options="keyUsageList"
        @change="(value) => handleChange(value, 'keyUsage')"
      ></form-item>
      <form-item
        label="安全计划处理阶段"
        v-model="beanInfo.secPlanStage"
        prop="secPlanStage"
        :inputType="select"
        :options="secPlanStageList"
        @change="(value) => handleChange(value, 'secPlanStage')"
      ></form-item>
      <form-item
        label="安全计划描述"
        v-model="beanInfo.secPlanDesc"
        prop="secPlanDesc"
      ></form-item>
      <form-item
        label="安全记划状态"
        v-model="beanInfo.secPlanStatus"
        prop="secPlanStatus"
        :inputType="select"
        :options="secPlanStatusList"
        @change="(value) => handleChange(value, 'secPlanStatus')"
      ></form-item>
      <form-item
        label="证书编号"
        v-model="beanInfo.certId"
        prop="certId"
      ></form-item>
      <form-item
        label="证书有效期"
        v-model="beanInfo.certExpiryDate"
        prop="certExpiryDate"
        :inputType="datepicker"
        @change="value => dateChange(value,'certExpiryDate')"
      ></form-item>
       <form-item
        label="算法类型"
        v-model="beanInfo.certAlgorithmType"
        prop="certAlgorithmType"
      ></form-item>
       <form-item
        label="证书发行机构"
        v-model="beanInfo.certIssueId"
        prop="certIssueId"
      ></form-item>
      <form-item
        label="证书签发日期"
        v-model="beanInfo.certIssueDate"
        prop="certIssueDate"
        :inputType="datepicker"
        @change="value => dateChange(value,'certIssueDate')"
      ></form-item>
      <form-item
        label="证书签发时间"
        v-model="beanInfo.certIssueTime"
        prop="certIssueTime"
        :inputType="timePicker"
        @change="value => timeChange(value,'certIssueTime')"
      ></form-item>
      <form-item
        label="证书描述"
        v-model="beanInfo.certDesc"
        prop="certDesc"
      ></form-item>
      <form-item
        label="证书序列号"
        v-model="beanInfo.certDn"
        prop="certDn"
      ></form-item>
      <form-item
        label="证书内容"
        v-model="beanInfo.certValue"
        prop="certValue"
      ></form-item>
      <form-item
        label="证书存储类型"
        v-model="beanInfo.certSaveType"
        prop="certSaveType"
        :inputType="select"
        :options="certSaveTypeList"
        @change="(value) => handleChange(value, 'certSaveType')"
      ></form-item>
    </div>
  </entity-view>
</template>
<script>
import EntityView from "../../../../components/common/entityView/EntityView.vue";
import FormItem from "../../../../components/common/entityView/FormItem.vue";
import moment from "moment";
moment.locale('zh-cn');

const keyUsage_C = [
  { value: "00", name: "数字签名证书公钥" },
  { value: "01", name: "敏感信息加密证书公钥" },
  { value: "10", name: "数字签名证书私钥" },
  { value: "11", name: "敏感信息加密证书私钥" },
];

const keyUsage_K = [
  { value: "00", name: "机构主密钥" },
  { value: "01", name: "敏感信息加密密钥" },
  { value: "02", name: "转PIN密钥" },
  { value: "03", name: "MAC签名密钥" },
];

export default {
  name: "PSecPlanOperation",
  components: {
    EntityView,
    FormItem,
  },
  props: {
    query: Object,
  },
  data: function () {
    return {
      modalSecTitle: "",
      operatorType: "edit",
      modalSecVisible: true,
      beanInfo: {

      },
      rules: {
        dstChannelTypeDesc: [
          { required: true, message: "必输", trigger: "blur" },
        ],
      },
      loading: false,
      pageInfo: {},
      keyTypeList: [
        { value: "C", name: "证书" },
        { value: "K", name: "密钥" },
      ],
      keyOprTypeList: [
        { value: "E", name: "加密" },
        { value: "D", name: "解密" },
        { value: "A", name: "加解密" },
      ],
      keyUsageList: [],
      secPlanStageList: [
        { value: "00", name: "报文解析前" },
        { value: "11", name: "报文组装后" },
        { value: "01", name: "报文解析后" },
        { value: "10", name: "报文组装前" },
      ],
      secPlanStatusList: [
        { value: "Y", name: "有效" },
        { value: "N", name: "无效" },
      ],
      certSaveTypeList: [
        { value: "0", name: "本地数据库存储" },
        { value: "1", name: "加密机" },
        { value: "2", name: "本地文件" },
      ],
    };
  },
  methods: {
    async submitDst() {
      let res;
      switch (this.operatorType) {
        case "edit":
          res = await this.edit();
          break;
        case "view":
          res = true;
          break;
        default:
          break;
      }
      return res;
    },
    async edit() {
      const res = await this.$refs.form.validate();
      if (res) {
        const params = this.beanInfo;
        params["optFlag"] = "upd";
        const res = await this.$put(
          "/admin/igas/pSecPlanInfo/editFromChnl",
          params
        );
        if (res.status === 200) {
          this.$message.success("保存成功！");
          return true;
        } else {
          this.$message.error(res.data.message);
          return false;
        }
      }
    },
    setOperatorType(type) {
      this.operatorType = type;
    },
    setBeanInfo(beanInfo) {
      this.beanInfo = beanInfo;
      this.getOne()
    },
   async getOne(){
      try {
            const res = await this.$get("/admin/igas/pSecPlanInfo/editFromChnl", {
              secPlanId: this.beanInfo.secPlanId,
              secPlanType: this.beanInfo.secPlanType,
              instId: this.beanInfo.instId,
              optFlag:'upd'
            });
            this.beanInfo = res.data || {};
          } catch (e) {
            console.error(e);
          }
    },
    handleChange(value, param) {
      this.beanInfo[param] = value;
      if (param === "keyType" && value === "C") {
        this.keyUsageList = keyUsage_C;
      } else if (param === "keyType" && value === "K") {
        this.keyUsageList = keyUsage_K;
      }
    },
     dateChange(value,param){
      this.beanInfo[param] = moment(value).format('YYYY-MM-DD');
    },
     timeChange(value,param){
      this.beanInfo[param] = moment(value).format('HH:mm:ss');
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../../../static/less/Common";

.subtitle {
  width: 100%;
  font-size: 18px;
  margin-bottom: 10px;
}
.sub-content-flex {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
.sub-content-block {
  display: block;
  flex-wrap: wrap;
  width: 100%;
}
</style>
